package com.chery.media.ui.setting

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.chery.media.R
import com.chery.media.databinding.ActivitySettingBinding
import com.chery.media.library.base.BaseVMActivity
import com.chery.media.library.extend.setNbOnItemClickListener
import com.chery.media.library.extend.statusBarTransparent

class SettingActivity: BaseVMActivity<SettingViewModel, ActivitySettingBinding>() {

    private val setAdapter by lazy {
        SettingAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                setSelect(position)
                switchPage(position)
            }
        }
    }

    private lateinit var showFragment: Fragment

    override fun initView(savedInstanceState: Bundle?) {
        statusBarTransparent()
        mBinding.settingType.adapter = setAdapter
        setAdapter.setList(viewModel.getSettingTypes())
        showFragment = viewModel.fragments[0]
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.container, showFragment)
            .commitNow()
    }

    override fun initData() {

    }

    private fun switchPage(index: Int) {
        val now = viewModel.fragments[index]
        supportFragmentManager.beginTransaction().apply {
            if (!now.isAdded) {
                add(R.id.container, now)
            }
            hide(showFragment)
            show(now)
            showFragment = now
            commit()
        }
    }

}